const _ = require("../import");

/**
 * @api {post} /type/list  list
 * @apiDescription 分类页面获取分类商品的接口
 * @apiName list
 * @apiGroup type
 * @apiParam {string} typename 分类的中文名称，例如：小米
 * @apiSuccessExample {json} 请求成功的返回:
 *  {
 *      "status" : 200,
 *      "message" : "请求成功"
 *      "result" : "asjXzXxmnSp8TZGWiTSlJF8Gb...5LhYoHs86SEI2LBXrTZ9e/a4frZOWHZ"
 *  }
 * @apiErrorExample {json} 请求失败的返回
 * {
 *   "status": 404,
 *   "message" : "请求失败"
 *   "result": null
 * }
 * @apiSampleRequest http://127.0.0.1:9090/type/list
 * @apiVersion 1.0.0
 */
_.router.post('/list', async function(req, res, next) {
  var typename = req.body.typename;

  var data = await _.db.query({
    sql: _.config.sql.getTypeList,
    data: [ typename ]
  });

  data.forEach(function (val,index) {
    val.wares_masterimg = JSON.parse(val.wares_masterimg)[0]
  });

  res.json({
    status: 200,
    message: '请求成功',
    result: data
  });

});

module.exports = _.router;
